package com.cheng.reggie.service.impl;

import com.cheng.reggie.common.R;
import com.cheng.reggie.entity.Category;
import com.cheng.reggie.entity.Page;
import com.cheng.reggie.exception.BusinessException;
import com.cheng.reggie.mapper.CategoryMapper;
import com.cheng.reggie.mapper.DishMapper;
import com.cheng.reggie.mapper.SetmealMapper;
import com.cheng.reggie.service.CategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired(required = false)
    private CategoryMapper categoryMapper;


    @Autowired(required = false)
    private DishMapper dishMapper;

    @Autowired(required = false)
    private SetmealMapper setmealMapper;


    /**
     * 根据type查询分类类别数据
     *
     * @param type
     * @return List
     */
    @Override
    public List<Category> findAllByType(Integer type) {
        //调用Mapper方法,把type封装进List
        return categoryMapper.findAllByType(type);
    }

    /**
     * 导出数据
     *
     * @return
     */
    @Override
    public List<Category> findAll() {
        return categoryMapper.findAll();
    }

    /**
     * 删除指定类别
     *
     * @param id
     * @return
     */
    @Override
    public boolean deleteById(Long id) {
        //1.获取类别菜品数量,如果大于1,则抛出异常"该类别有菜品无法删除"
        Integer dishCount = dishMapper.countByCid(id);
        if (dishCount>0){
            throw  new BusinessException("该类别有菜品无法删除");
        }
        //2.获取套餐信息数量,如果大于1,则抛出异常"该类别有套餐信息无法删除"
        Integer setMealCount = setmealMapper.countByCid(id);
        if (setMealCount>0){
            throw  new BusinessException("该类别有套餐信息无法删除");
        }

        //3.调用数据访问层删除
        return  categoryMapper.deleteById(id)>0;

    }

    /**
     * 新增菜品方法
     *
     * @param category
     * @return boolean
     */
    @Override
    public boolean save(Category category) {
        //完善数据
        //创建时间
        category.setCreateTime(LocalDateTime.now());
        //修改时间
        category.setUpdateTime(LocalDateTime.now());
        //判断类别名称是否存在
        Category dbCategory = categoryMapper.findName(category.getName());
        if (dbCategory != null){
            throw new BusinessException("分类名称已经存在,请修改");
        }
        return categoryMapper.save(category)>0;
    }

    /**
     * 类别分页管理
     *
     * @param page
     * @param pageSize
     * @return Page
     */
    @Override
    public Page<Category> findByPage(Integer page, Integer pageSize) {
        //1.设置分页查询第几页和每页多少条
        PageHelper.startPage(page,pageSize);
        //2.查询数据
        List<Category> categoryList = categoryMapper.findAll();
        //3.创建pageInfo封装上面结果集
        PageInfo<Category> pageInfo = new PageInfo<>(categoryList);
        //把pageInfo数据封装到pageObj对象中
        Page<Category> pageObj = new Page<>();
        pageObj.setRecords(pageInfo.getList());
        pageObj.setTotal(pageInfo.getTotal());

        return pageObj;
    }

    /**
     * 类别修改管理
     *
     * @param category
     * @return boolean
     */
    @Override
    public boolean update(Category category ) {
        //判断类别名称是否重复,如果冲突提示用户"已存在相同分类名称,修改失败"
        //给家类别查询类别数,排除自己
        Category categoryName = categoryMapper.findByName2(category);
        if (categoryName != null) {
            throw  new BusinessException("已存在相同分类名称,修改失败");
        }
        //调用mapper的修改sql语句
        return categoryMapper.updateById(category)>0;
    }


}
